using System;
using System.Collections;
using System.Data;
using System.Data.OleDb;
using Smallpointer.Data.Common;

namespace Smallpointer.Data.Access
{
    internal class AccessUtil : DBase
    {
        private readonly Hashtable parmCache = Hashtable.Synchronized(new Hashtable());
        public OleDbConnection SqlConn = new OleDbConnection();

        public AccessUtil(Data parent)
            : base(parent)
        {
            SqlConn = new OleDbConnection(Parent.DBConfig.Connstring);
        }

        public int IExecuteNonQuery(CommandType cmdType, string cmdText, params OleDbParameter[] commandParameters)
        {
            var cmd = new OleDbCommand();
            PrepareCommand(cmd, null, cmdType, cmdText, commandParameters);
            int val = cmd.ExecuteNonQuery();
            cmd.Parameters.Clear();
            return val;
        }


        public int ExecuteNonQuery(CommandType cmdType, string cmdText, params OleDbParameter[] commandParameters)
        {
            var cmd = new OleDbCommand();
            PrepareCommand(cmd, null, cmdType, cmdText, commandParameters);
            int val = cmd.ExecuteNonQuery();
            cmd.Parameters.Clear();
            return val;
        }


        public int ExecuteNonQuery(OleDbTransaction trans, CommandType cmdType, string cmdText, params OleDbParameter[] commandParameters)
        {
            var cmd = new OleDbCommand();
            PrepareCommand(cmd, trans, cmdType, cmdText, commandParameters);
            int val = cmd.ExecuteNonQuery();
            cmd.Parameters.Clear();
            return val;
        }


        public OleDbDataReader ExecuteReader(CommandType cmdType, string cmdText, params OleDbParameter[] commandParameters)
        {
            var cmd = new OleDbCommand();
            OleDbConnection conn = SqlConn;
            try
            {
                PrepareCommand(cmd, null, cmdType, cmdText, commandParameters);
                OleDbDataReader rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
                cmd.Parameters.Clear();
                return rdr;
            }
            catch
            {
                conn.Close();
                throw;
            }
        }

        public DataSet ExecuteDataSet(CommandType cmdType, string cmdText, params OleDbParameter[] commandParameters)
        {
            var cmd = new OleDbCommand();
            var conn = new OleDbConnection(SqlConn.ConnectionString);
            var ds = new DataSet();

            PrepareCommand(cmd, null, cmdType, cmdText, commandParameters);
            OleDbDataReader rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
            try
            {
                cmd.Parameters.Clear();
                if (rdr != null)
                {
                    ds.Load(rdr, LoadOption.OverwriteChanges, new[] { "ds" });
                    rdr.Close();
                }
            }
            catch
            {
                if (rdr != null)
                {
                    if (!rdr.IsClosed)
                        rdr.Close();
                }
                conn.Close();
                throw;
            }
            return ds;
        }


        public object ExecuteScalar(CommandType cmdType, string cmdText, params OleDbParameter[] commandParameters)
        {
            var cmd = new OleDbCommand();
            PrepareCommand(cmd, null, cmdType, cmdText, commandParameters);
            object val = cmd.ExecuteScalar();
            cmd.Parameters.Clear();
            return val;
        }


        public void CacheParameters(string cacheKey, params OleDbParameter[] commandParameters)
        {
            parmCache[cacheKey] = commandParameters;
        }


        public OleDbParameter[] GetCachedParameters(string cacheKey)
        {
            var cachedParms = (OleDbParameter[])parmCache[cacheKey];

            if (cachedParms == null)
                return null;


            var clonedParms = new OleDbParameter[cachedParms.Length];


            for (int i = 0, j = cachedParms.Length; i < j; i++)

                clonedParms = (OleDbParameter[])((ICloneable)cachedParms).Clone();

            return clonedParms;
        }


        private void PrepareCommand(OleDbCommand cmd, OleDbTransaction trans, CommandType cmdType, string cmdText, OleDbParameter[] cmdParms)
        {
            if (SqlConn.State != ConnectionState.Open)
                SqlConn.Open();

            cmd.Connection = SqlConn;
            cmd.CommandText = cmdText;


            if (trans != null)
                cmd.Transaction = trans;

            cmd.CommandType = cmdType;

            if (cmdParms == null) return;
            foreach (OleDbParameter parm in cmdParms)
                cmd.Parameters.Add(parm);
        }

        public int ExecuteScript(string cmdText)
        {
            int i;

            if (SqlConn.State != ConnectionState.Open)
                SqlConn.Open();
            try
            {
                var cmd = new OleDbCommand(cmdText, SqlConn);
                i = cmd.ExecuteNonQuery();
            }
            finally
            {
                SqlConn.Close();
            }
            return i;
        }

        public void CleanConn()
        {
            SqlConn.Dispose();
            SqlConn.Close();
            SqlConn = null;
        }


        ~AccessUtil()
        {
            Dispose();
        }

        public override void Dispose()
        {
        }
    }
}